Skip to contentSkip to conversation

Change in openvpn[master]: Avoid unbounded allocations in pkcs11_mbedtls.c

Openvpn-devel In review MaxF (Code Review) 2 participants 9 messages Feb 20 – Mar 2 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1771607942000.I53d47e4a0d33c380ee95e0e33aecad3db3197940@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email

to review the following change.

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Avoid unbounded allocations in pkcs11_mbedtls.c

The PKCS#11 provider can crash OpenVPN by making it try to allocate 2^64 bytes for a certificate. To avoid this, set a maximum size for certificates. If the size is exceeded, don't try to allocate memory and instead exit pkcs11_get_x509_cert with an error.

The chosen maximum size is 100.000 bytes which is twice the size of a SLH-DSA (aka SPHINCS+) signature.

Github: closes OpenVPN/openvpn-private-issues#42

Change-Id I53d47e4a0d33c380ee95e0e33aecad3db3197940

---
M src/openvpn/pkcs11_mbedtls.c

1 file changed, 16 insertions(+), 0 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/49/1549/1

src/openvpn/pkcs11_mbedtls.c +16 -0
index 66aefac..bf9d953 100644
@@ -42,6 +42,16 @@
 static bool
 pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert)
 {
+    /* We set a maximum size for certificates so that the PKCS provider cannot crash OpenVPN by
+     * making it try to allocate 2^64 bytes. The maximum of 100.000 bytes is picked as a round
+     * number that easily accomodates the currently standardized quantum-safe signature algorithms.
+     * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
+     *
+     * However, there are additional digital signature schemes currently on the NIST on-ramp
+     * (e.g., some parameter settings for LESS) that have even larger public keys or signatures, so
+     * if those ever see use on smartcards, we will need to increase this number. */
+    const size_t max_cert_size = 100000;
+
     unsigned char *cert_blob = NULL;
     size_t cert_blob_size = 0;
     bool ret = false;
@@ -52,6 +62,12 @@
         goto cleanup;
     }
 
+    if (cert_blob_size > max_cert_size)
+    {
+        msg(M_WARN, "PKCS#11: Certificate too large: %lu bytes, maximum is %lu", cert_blob_size, max_cert_size);
+        goto cleanup;
+    }
+
     check_malloc_return((cert_blob = calloc(1, cert_blob_size)));
     if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, &cert_blob_size) != CKR_OK)
     {

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, plaisthos.

syzzer has posted comments on this change by MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Patch Set 1:

(1 comment)

Patchset:

PS1:
The change itself looks good. And might even be good practice. But isn't this a bit moot since the pkcs11 provider is loaded as a shared lib and can thus crash us any way they like?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Fri, 20 Feb 2026 19:04:28 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos, syzzer.

MaxF has posted comments on this change by MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Patch Set 1:

(2 comments)

Patchset:

PS1:
I don't understand why the tests are failing. Some of them don't even use mbedtls. Can someone else take a look?

PS1:

Quoted 1 line

The change itself looks good. And might even be good practice. […]

You're right, that's why this is not a CVE. I discussed it with Gert in the private-issues repo and we agreed to label it as "minibug". We can't protect against a malicious pkcs11 provider, but maybe we can be more robust against a buggy one.

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>
Gerrit-Attention: syzzer <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Sun, 22 Feb 2026 22:14:40 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: syzzer <[email protected]...>

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, plaisthos, syzzer.

cron2 has posted comments on this change by MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Patch Set 1:

(1 comment)

Patchset:

PS1:

Quoted 1 line

I don't understand why the tests are failing. Some of them don't even use mbedtls. […]

I broke the servers, just when you pushed. Have clicked "rerun".

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>
Gerrit-Attention: syzzer <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Sun, 22 Feb 2026 22:17:00 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: MaxF <[email protected]...>

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: MaxF, syzzer.

plaisthos has posted comments on this change by MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Patch Set 1: Code-Review+2

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 1
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>
Gerrit-Attention: syzzer <[email protected]...>
Gerrit-Attention: MaxF <[email protected]...>
Gerrit-Comment-Date: Mon, 02 Mar 2026 14:19:58 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

#6 lore raw

PATCH 1/1 Avoid unbounded allocations in pkcs11_mbedtls.c

In reply to a message not in the archive

From: Max Fillinger <[email protected]...>

The PKCS#11 provider can crash OpenVPN by making it try to allocate 2^64 bytes for a certificate. To avoid this, set a maximum size for certificates. If the size is exceeded, don't try to allocate memory and instead exit pkcs11_get_x509_cert with an error.

The chosen maximum size is 100.000 bytes which is twice the size of a SLH-DSA (aka SPHINCS+) signature.

Github: closes OpenVPN/openvpn-private-issues#42

Change-Id I53d47e4a0d33c380ee95e0e33aecad3db3197940

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1549
---

This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1549
This mail reflects revision 1 of this Change.

Acked-by according to Gerrit (reflected above):
Arne Schwabe <[email protected]...>

src/openvpn/pkcs11_mbedtls.c +16 -0
index 66aefac..bf9d953 100644
@@ -42,6 +42,16 @@
 static bool
 pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert)
 {
+    /* We set a maximum size for certificates so that the PKCS provider cannot crash OpenVPN by
+     * making it try to allocate 2^64 bytes. The maximum of 100.000 bytes is picked as a round
+     * number that easily accomodates the currently standardized quantum-safe signature algorithms.
+     * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
+     *
+     * However, there are additional digital signature schemes currently on the NIST on-ramp
+     * (e.g., some parameter settings for LESS) that have even larger public keys or signatures, so
+     * if those ever see use on smartcards, we will need to increase this number. */
+    const size_t max_cert_size = 100000;
+
     unsigned char *cert_blob = NULL;
     size_t cert_blob_size = 0;
     bool ret = false;
@@ -52,6 +62,12 @@
         goto cleanup;
     }
 
+    if (cert_blob_size > max_cert_size)
+    {
+        msg(M_WARN, "PKCS#11: Certificate too large: %lu bytes, maximum is %lu", cert_blob_size, max_cert_size);
+        goto cleanup;
+    }
+
     check_malloc_return((cert_blob = calloc(1, cert_blob_size)));
     if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, &cert_blob_size) != CKR_OK)
     {
#7 lore raw

[Openvpn-devel] [PATCH applied] Re: Avoid unbounded allocations in pkcs11_mbedtls.c

This is somewhat of a "minibug", but we decided that we want to have a bit of a safeguard against a pkcs#11 provider that requests us to allocate impossible amounts of memory. Of course there are other ways it could crash, but this one is a bit of "good housekeeping".

I have not tested it beyond "does it compile" - have no smartcard, and especially not one that misbehaves (and do not feel like setting up SoftHSM and stuff to test this for real).

Your patch has been applied to the master and release/2.7 branch (minibug).

commit 0a8e80aaf9c96718903251a828bc3e8055014160 (master) commit bf7f8548c7bfd31f0e6fed880890d5106c2ab982 (release/2.7)

Author: Max Fillinger
Date:   Mon Mar 2 15:20:39 2026 +0100
     Avoid unbounded allocations in pkcs11_mbedtls.c

     Signed-off-by: Max Fillinger <[email protected]...>
     Acked-by: Arne Schwabe <[email protected]...>
     Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1549
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg35807.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#8 lore raw
In reply to a message not in the archive

cron2 has uploaded a new patch set (#2) to the change originally created by MaxF. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by plaisthos

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Avoid unbounded allocations in pkcs11_mbedtls.c

The PKCS#11 provider can crash OpenVPN by making it try to allocate 2^64 bytes for a certificate. To avoid this, set a maximum size for certificates. If the size is exceeded, don't try to allocate memory and instead exit pkcs11_get_x509_cert with an error.

The chosen maximum size is 100.000 bytes which is twice the size of a SLH-DSA (aka SPHINCS+) signature.

Github: closes OpenVPN/openvpn-private-issues#42

Change-Id I53d47e4a0d33c380ee95e0e33aecad3db3197940

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1549
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35807.html

---
M src/openvpn/pkcs11_mbedtls.c

1 file changed, 16 insertions(+), 0 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/49/1549/2

src/openvpn/pkcs11_mbedtls.c +16 -0
index 66aefac..bf9d953 100644
@@ -42,6 +42,16 @@
 static bool
 pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert)
 {
+    /* We set a maximum size for certificates so that the PKCS provider cannot crash OpenVPN by
+     * making it try to allocate 2^64 bytes. The maximum of 100.000 bytes is picked as a round
+     * number that easily accomodates the currently standardized quantum-safe signature algorithms.
+     * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
+     *
+     * However, there are additional digital signature schemes currently on the NIST on-ramp
+     * (e.g., some parameter settings for LESS) that have even larger public keys or signatures, so
+     * if those ever see use on smartcards, we will need to increase this number. */
+    const size_t max_cert_size = 100000;
+
     unsigned char *cert_blob = NULL;
     size_t cert_blob_size = 0;
     bool ret = false;
@@ -52,6 +62,12 @@
         goto cleanup;
     }
 
+    if (cert_blob_size > max_cert_size)
+    {
+        msg(M_WARN, "PKCS#11: Certificate too large: %lu bytes, maximum is %lu", cert_blob_size, max_cert_size);
+        goto cleanup;
+    }
+
     check_malloc_return((cert_blob = calloc(1, cert_blob_size)));
     if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, &cert_blob_size) != CKR_OK)
     {

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 2
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>

#9 lore raw
In reply to a message not in the archive

cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email )

Change subject: Avoid unbounded allocations in pkcs11_mbedtls.c
......................................................................

Avoid unbounded allocations in pkcs11_mbedtls.c

The PKCS#11 provider can crash OpenVPN by making it try to allocate 2^64 bytes for a certificate. To avoid this, set a maximum size for certificates. If the size is exceeded, don't try to allocate memory and instead exit pkcs11_get_x509_cert with an error.

The chosen maximum size is 100.000 bytes which is twice the size of a SLH-DSA (aka SPHINCS+) signature.

Github: closes OpenVPN/openvpn-private-issues#42

Change-Id I53d47e4a0d33c380ee95e0e33aecad3db3197940

Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1549
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35807.html

---
M src/openvpn/pkcs11_mbedtls.c

1 file changed, 16 insertions(+), 0 deletions(-)
src/openvpn/pkcs11_mbedtls.c +16 -0
index 66aefac..bf9d953 100644
@@ -42,6 +42,16 @@
 static bool
 pkcs11_get_x509_cert(pkcs11h_certificate_t pkcs11_cert, mbedtls_x509_crt *cert)
 {
+    /* We set a maximum size for certificates so that the PKCS provider cannot crash OpenVPN by
+     * making it try to allocate 2^64 bytes. The maximum of 100.000 bytes is picked as a round
+     * number that easily accomodates the currently standardized quantum-safe signature algorithms.
+     * It is twice the size of a SLH-DSA (aka SPHINCS+) signature plus public key.
+     *
+     * However, there are additional digital signature schemes currently on the NIST on-ramp
+     * (e.g., some parameter settings for LESS) that have even larger public keys or signatures, so
+     * if those ever see use on smartcards, we will need to increase this number. */
+    const size_t max_cert_size = 100000;
+
     unsigned char *cert_blob = NULL;
     size_t cert_blob_size = 0;
     bool ret = false;
@@ -52,6 +62,12 @@
         goto cleanup;
     }
 
+    if (cert_blob_size > max_cert_size)
+    {
+        msg(M_WARN, "PKCS#11: Certificate too large: %lu bytes, maximum is %lu", cert_blob_size, max_cert_size);
+        goto cleanup;
+    }
+
     check_malloc_return((cert_blob = calloc(1, cert_blob_size)));
     if (pkcs11h_certificate_getCertificateBlob(pkcs11_cert, cert_blob, &cert_blob_size) != CKR_OK)
     {

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1549?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I53d47e4a0d33c380ee95e0e33aecad3db3197940
Gerrit-Change-Number: 1549
Gerrit-PatchSet: 2
Gerrit-Owner: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: syzzer <[email protected]...>